package com.premadefood.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.premadefood.common.ErrorCode;
import com.premadefood.domain.*;
import com.premadefood.exception.BusinessException;
import com.premadefood.service.NewfocusService;
import com.premadefood.service.NewsService;
import com.premadefood.mapper.NewsMapper;
import com.premadefood.service.NewsviewhistoryService;
import com.premadefood.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 23854
* @description 针对表【news】的数据库操作Service实现
* @createDate 2024-02-27 16:10:49
*/
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News>
    implements NewsService {

    @Resource
    private UserService userService;

    @Resource
    private NewfocusService newfocusService;

    @Resource
    private NewsviewhistoryService newsviewhistoryService;


    @Override
    public List<News> getFocusNews(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Integer id = loginUser.getId();
        QueryWrapper<Newfocus> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", id);
        List<Newfocus> newfocusList = newfocusService.list(queryWrapper);
        List<String> authorList = newfocusList.stream()
                .map(Newfocus::getAuthor).collect(Collectors.toList());
        QueryWrapper<News> newsQueryWrapper = new QueryWrapper<>();
        newsQueryWrapper.in("author",authorList);
        return this.list(newsQueryWrapper);
    }

    @Override
    public List<News> searchNewsByTitle(String searchContext) {
        if(StringUtils.isBlank(searchContext)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"搜索参数为空");
        }
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title",searchContext);
        return this.list(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public News getDetail(Integer newsId,HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Integer userId = loginUser.getId();
        News news = this.getById(newsId);
        //资讯查看历史表新增记录
        //先查询是否已有查看记录，如果有记录，则刷新它的更新时间
        QueryWrapper<Newsviewhistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",loginUser.getId()).eq("newsId",newsId);
        Newsviewhistory newsviewhistory = newsviewhistoryService.getOne(queryWrapper);
        if(newsviewhistory != null){
            newsviewhistory.setUpdateTime(new Date());
            newsviewhistoryService.updateById(newsviewhistory);
        }
        else{
            Newsviewhistory newNewsviewhistory = new Newsviewhistory();
            newNewsviewhistory.setNewsId(newsId);
            newNewsviewhistory.setUserId(userId);
            newsviewhistoryService.save(newNewsviewhistory);
        }
        return news;
    }

    @Override
    public List<News> viewedHistoryNews(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Integer userId = loginUser.getId();
        QueryWrapper<Newsviewhistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId).orderByDesc("updateTime");
        List<Integer> newsIds = newsviewhistoryService.list(queryWrapper).stream()
                .map(Newsviewhistory::getNewsId).collect(Collectors.toList());
        QueryWrapper<News> newsQueryWrapper = new QueryWrapper<>();
        newsQueryWrapper.in("id",newsIds);
        return this.list(newsQueryWrapper);

    }
}




